package priv.pront.code.mati.province.comp;

import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-11-18 15:51
 */
public class Main10 {

    static int n, m, cnt = 0;
    static int[][] g;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        g = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                g[i][j] = scanner.nextInt();
            }
        }
        int i = 0, j = 0;
        while (i < n && j < m) {
            if (!check(g[i][j])) {
                if (j + 1 >= m) {
                    j = 0;
                    i++;
                } else {
                    j++;
                }
            }else{
                cnt++;
                int t = g[i][j] % 10;
                if(j + t >= m){
                    j = 0;
                    i++;
                }else{
                    j += t;
                }
            }
        }
        System.out.println(cnt);

    }

    private static boolean check(int a) {
        boolean flag = true;
        int target = (int) Math.sqrt(a);
        for (int i = 2; i <= target; i++) {
            if (a % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}
